# 屏幕显示及发声模块
print('------')
from mpython import *
myUI = UI(oled)
import music
import math
import machine
print('dice.py')

def fresh_(fresh_=0):
    if fresh_ == 0:
        oled.fill(0)
    elif fresh_ == 1:
        oled.show()

def text_(text="Text to show", x=0, y=0, mode=0, wrap=False, interval=16):
    for char in text:
        if x + interval > 128:
            # Check if there's enough space for '...'
            if x <= 128 - interval:
                oled.DispChar('...', 128 - interval, y, mode, wrap)  # 添加省略号 "..."
            break  # 停止循环，因为已经添加了省略号
        oled.DispChar(char, x, y, mode, wrap)
        x += interval  # 移动到下一个字符的位置

def rect_(x=0, y=0, width=0, height=0, r=0, mode=1):
    if mode == 1:
        oled.RoundRect(x, y, width, height, r, 1)
    elif mode == 0:
        # 创建frame buffer对象
        buf = bytearray((oled.width // 8) * oled.height)
        fb = framebuf.FrameBuffer(buf, oled.width, oled.height, framebuf.MVLSB)
        # 绘制一个与矩形相同大小的填充色背景
        fb.fill_rect(x, y, width, height, 0)
        # 在上面绘制一个白色的圆角矩形
        fb.fill_rect(x + r, y, width - 2*r, height, 1)
        fb.fill_rect(x, y + r, r, height - 2*r, 1)
        fb.fill_rect(x + width - r, y + r, r, height - 2*r, 1)
        fb.fill_circle(x + r, y + r, r, 1)
        fb.fill_circle(x + width - r - 1, y + r, r, 1)
        fb.fill_circle(x + r, y + height - r - 1, r, 1)
        fb.fill_circle(x + width - r - 1, y + height - r - 1, r, 1)
        oled.blit(fb, x, y)

def text_line_(lin1='', lin2='', lin3='', lin4=''):
    oled.DispChar(lin1, 0, 0, 1)
    oled.DispChar(lin2, 0, 16, 1)
    oled.DispChar(lin3, 0, 32, 1)
    oled.DispChar(lin4, 0, 48, 1)

def circle_(x=0, y=0, r=0, mode=0):
    if mode == 0:
        oled.circle(x, y, r, 1)
    elif mode == 1:
        oled.fill_circle(x, y, r, 1)

def tring_(x0=0, y0=0, x1=1, y1=1, x2=2, y2=2, mode=0):
    if mode == 0:
        oled.triangle(x0, y0, x1, y1, x2, y2, 1)
    elif mode == 1:
        oled.fill_triangle(x0, y0, x1, y1, x2, y2, 1)

def bip_(x, y, data, w, h):
    oled.bitmap(x, y, data, w, h, 1)

def qrc_(content_, x, y, size='s'):
    if size == 's':
        myUI.qr_code('content_', x, y, scale=1)
    elif size == 'S':
        myUI.qr_code('content_', x, y, scale=2)

def fun_(x=5, y=5, xmin_=0, xmax_=0, coef1_=0, coef2_=0, coef3_=0, freq=1, mode=1):
    if freq == 1:
        for interval in range(xmax_ - xmin_):
            result = coef1_ * interval + coef3_
            oled.pixel(x+interval, y+result, mode)
            if coef1_ != 1: #   绘制连贯曲线，暂未完成
                if coef1_<0:
                    oled.vline(x+interval, y+result, coef1_, mode)
                elif coef1_>0:
                    oled.pixel(x+interval, y+result-coef1_, coef1_, mode)
            else:
                pass
    elif freq == 2:
        for interval in range(xmax_ - xmin_):
            result = coef1_ * coef1_ * interval + coef2_ * interval + coef3_
            oled.pixel(x+interval, y+result, mode)
    elif freq == 3:
        for interval in range(xmax_ - xmin_):
            result = coef1_ * coef1_ * coef1_ * interval + coef3_
            oled.pixel(x+interval, y+result, mode)

def his_(x=5, y=5, data_=[0], width=20, height=10): #   绘制柱状图
    for data in len(data_):
        pass

def shw_():# 阴影绘制
    def upRange(start, stop, step):
        while start <= stop:
            yield start
            start += abs(step)
    y = 59
    for count in range(5):
        for i in upRange:
            oled.pixel(i, y, 1)
        y = y + 1
    oled.show()

def f_bur_(pitch_='C3', length=1, float=0):
    if float == 0:
        music.play('pitch:length')
    elif float == 1:
        length += math.floor(0.5 * length) * 4
        music.play('pitch:length')